// pages/index/index-nearby/index-nearby.js
var request = require('../../../utils/request.js');
var util = require('../../../utils/util.js');
// 引入SDK核心类
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 选择地址
    location: '',
    lat: '',
    lng: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //调用当前位置
    that.getToken();
  },
  onShow:function(){
  
  },
  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        that.loadAddress(res.data);
      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },
  loadAddress: function (param_token) {
    //页面展示选择地址
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // 成功返回函数
        console.log(res)
        var longitude = res.longitude;
        var latitude = res.latitude;

        // 实例化API核心类
        var demo = new QQMapWX({
          key: 'UBGBZ-EWK3Q-C2Z5A-G3HJT-NOWSH-4BFIF' // 必填
        });
        // 调用接口
        demo.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            console.log(res);
           
            that.setData({
              location: res.result.address,
              lat: res.result.ad_info.location.lat,
              lng: res.result.ad_info.location.lng,
            })
            getApp().globalData.location = res.result.address
            console.log(getApp().globalData.location)
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        });

        that.getNearCityList(param_token,latitude, longitude);
      },
      fail: function (res) {
        console.log(res)
        wx.getSetting({
          success(res) {
            if (!res.authSetting['scope.userLocation']) {
              wx.showModal({
                title: '友情提示',
                content: '请允许获取地理信息，来获取附近书柜',
                showCancel: false,
                confirmText: "允许",
                confirmColor: "rgb(49,188,0)",
                success: function (res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success: (res) => {
                        res.authSetting = {
                          "scope.userLocation": true
                        }
                        that.loadAddress(param_token);
                      },
                      fail: function (res) {
                        console.log(res)
                      }
                    })
                  } else if (res.cancel) {
                    setTimeout(function () {
                      wx.showToast({
                        title: '未允许获取信息',
                        image: "../../images/xx.png"
                      })
                    }, 800)
                  }
                }
              })
            }
          }
        })

      }
    })

  },
  goSearch:function(e){
    console.log(e.currentTarget.dataset.id)

    wx:wx.navigateTo({
      url: '../case/case?id=' + e.currentTarget.dataset.id,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })

  },
  getNearCityList: function (param_token,param_lat,param_lng){
    var that = this;
    var postData = {};
    postData.session_id = param_token;
    postData.lat = param_lat;
    postData.lng = param_lng;
    // 获取附近的列表
    request.getBookStore(postData,
      (res) => {
        console.log(res.data)
        wx.hideLoading()
        if (res.data != "" && res.data != [] && res.data != null && res.data.length > 0) {
          that.setData({
            list: res.data,
            showTip: false
          })
          for (var i = 0; i < res.data.length; i++) {
            that.data.list[i].address = util.cutTitleString(res.data[i].tname, 0, 10)
          }
          that.setData({
            list: that.data.list
          })
        } else {
          that.setData({
            showTip: true
          })
        }

      }
    )
  },
  //打开地址
  goMap: function (e) {
    // console.log(e.currentTarget.dataset)
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        wx.openLocation({
          latitude: parseFloat(e.currentTarget.dataset.lat),
          longitude: parseFloat(e.currentTarget.dataset.lng),
          scale: 28,
          name: e.currentTarget.dataset.addr
        })
      }
    })
  },
  chooseAddr:function(){
    var that= this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res)
        that.setData({
          location: res.address,
          latitude: res.latitude,
          longitude:res.longitude
        })
        that.getNearCityList(that.data.token,res.latitude, res.longitude);
      }
    })
  }
})